
package com.fly.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fly.core.entity.PageEntity;
import com.fly.core.entity.PageUtils;
import com.fly.demo.dao.UserDAO;
import com.fly.demo.entity.User;
import com.fly.demo.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserDAO userDAO;
    
    @Override
    public User get(Long id)
    {
        return userDAO.getOne(id);
    }
    
    @Override
    public void saveOrUpdate(User user)
    {
        userDAO.save(user);
    }
    
    @Override
    public void delete(Long id)
    {
        userDAO.deleteById(id);
    }
    
    @Override
    public Page<User> findAll(PageEntity page)
    {
        return userDAO.findAll(PageUtils.createPageable(page));
    }
    
    @Override
    public List<User> findByExample(Specification<User> specification, PageEntity page)
    {
        Page<User> springDataPage = userDAO.findAll(specification, PageUtils.createPageable(page));
        page.setTotalCount(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }
    
    @Override
    public List<User> findAll()
    {
        return userDAO.findAll();
    }
}
